package com.jy.adscm.operation.base.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.QueryTaskWorkPlan;
import com.jy.adscm.operation.base.bean.TaskWorkPlan;
import com.jy.adscm.operation.base.service.TaskWorkPlanService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 个人工作计划 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-05-26
 */
@RestController
@RequestMapping("/adscm/operation/taskWorkPlan")
public class TaskWorkPlanController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    TaskWorkPlanService taskWorkPlanService;
    @Autowired
    JyUserMapper jyUserMapper;
    @AdscmLogger("新增个人计划")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody TaskWorkPlan taskWorkPlan, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = taskWorkPlanService.insertTaskWorkPlan(taskWorkPlan,token);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @AdscmLogger("修改个人计划")
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody TaskWorkPlan taskWorkPlan, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = taskWorkPlanService.updateTaskWorkPlan(taskWorkPlan,token);
        return new SimpleCoreResponse("200","修改成功",insert);
    }
    @AdscmLogger("删除个人计划")
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Long id, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = taskWorkPlanService.deleteTaskWorkPlan(id,token);
        return new SimpleCoreResponse("200","删除成功",insert);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryTaskWorkPlan queryTaskWorkPlan, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
         queryTaskWorkPlan.setCreator(token.getUser().getId().toString());
         Page<TaskWorkPlan>  page= taskWorkPlanService.select(queryTaskWorkPlan,token);
        return new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectApprove")
    public SimpleCoreResponse selectApprove(@RequestBody QueryTaskWorkPlan queryTaskWorkPlan, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean approvor = jyUserMapper.queryUserIsHasRole(token.getUser().getId().toString(), "231");

        Boolean  deptApprovor = jyUserMapper.queryUserIsHasRole(token.getUser().getId().toString(), "234");

        if (!approvor &&!deptApprovor){
            queryTaskWorkPlan.setCreator(token.getUser().getId().toString());
        }
        if (deptApprovor){
            queryTaskWorkPlan.setDeptId(token.getUser().getDeptId());
        }

         Page<TaskWorkPlan>  page= taskWorkPlanService.select(queryTaskWorkPlan,token);
        return new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectCc")
    public SimpleCoreResponse selectCc(@RequestBody QueryTaskWorkPlan queryTaskWorkPlan, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
         Page<TaskWorkPlan>  page= taskWorkPlanService.selectCc(queryTaskWorkPlan,token);
        return new SimpleCoreResponse("200","查询成功",page);
    }

}

